#include "Controller.h"

CController::CController(void)
{
	m_bIsInput = true;
	m_Input = NONE;
	m_iWidth = 0;
	m_iHeight = 0;
	m_bIsDown = false;
}

CController::~CController(void)
{
}

int CController::Initialization(int width, int height)
{
	m_iWidth = width;
	m_iHeight = height;

	m_handle = GetStdHandle(STD_OUTPUT_HANDLE);

	if (FAIL == SetTitle())
	{
		cout << "Fail to set Title" << endl;
		return FAIL;
	}


	if (FAIL == SetDisplayMode())
	{
		cout << "Fail to set display mode" << endl;
		return FAIL;
	}

	// initial render class
	m_Render.Initialization(width, height);
	// initial model class
	m_Model.initialization(width, height);

	return OK;
}

int CController::SetTitle()
{
	TCHAR oldTitle[MAX_PATH];
	TCHAR newTitle[MAX_PATH];
	if (GetConsoleTitle(oldTitle, MAX_PATH))
	{
		wsprintf(newTitle, TEXT("Theft"));// %s"));//, oldTitle);

		if (!SetConsoleTitle(newTitle))
		{
			cout << "Fail to set new title" << endl;
			return FAIL;
		}
	}
	else
	{
		cout << "Fail to set title" << endl;
		return FAIL;
	}

	return OK;
}

int CController::SetDisplayMode()
{
	SMALL_RECT rect = {0, 0, 0, 0};
	COORD buffer;
	CONSOLE_SCREEN_BUFFER_INFO info;
/*
	GetConsoleScreenBufferInfo(m_handle, &info);

	buffer.X = info.dwSize.X;
	buffer.Y = info.dwSize.Y;
	rect.Bottom = info.srWindow.Bottom;
	rect.Right = info.srWindow.Right;
	cout << rect.Bottom << " " << rect.Right;
*/
	// initial the fixed screen size
	rect.Bottom = m_iHeight;
	rect.Right = m_iWidth;
	
	if (!SetConsoleWindowInfo(m_handle, true, &rect))
	{
		cout << "Fail set console window info" << endl;
		return FAIL;
	}

	GetConsoleScreenBufferInfo(m_handle, &info);
	buffer.X = info.srWindow.Right + 1;
	buffer.Y = info.srWindow.Bottom + 1;

	if (!SetConsoleScreenBufferSize(m_handle, buffer))
	{
		cout << "Fail to set buffer size" << endl;
		return FAIL;
	}

	return OK;
}

int CController::Input()
{
	m_bIsInput = false;
	//m_Input = NONE;
	while(!m_bIsInput)
	{
		// check if the user releases the LEFT arrow key
		if (GetKeyState(86) < 0x80000000 && m_bIsDown && m_Input == LETTER_V)
		{
			m_Input = NONE;
			m_bIsDown = false;
		}
		// Check if the user presses the LEFT arrow key
		else if (GetKeyState(86) < 0 && !m_bIsDown)
		{
			m_Input = LETTER_V;
			m_bIsInput = true;
			m_bIsDown = true;			
		}
		// check if the user releases the s key
		else if (GetKeyState(83) < 0x80000000 && m_bIsDown && m_Input == LETTER_S)
		{
			m_Input = NONE;
			m_bIsDown = false;
		}
		// Check if the user presses the s key
		else if (GetKeyState(83) < 0 && !m_bIsDown)
		{
			m_Input = LETTER_S;
			m_bIsInput = true;
			m_bIsDown = true;			
		}
		// check if the user releases the 1 key
		else if (GetKeyState(49) < 0x80000000 && m_bIsDown && m_Input == NUM_1)
		{
			m_Input = NONE;
			m_bIsDown = false;
		}
		// Check if the user presses the 1 key
		else if (GetKeyState(49) < 0 && !m_bIsDown)
		{
			m_Input = NUM_1;
			m_bIsInput = true;
			m_bIsDown = true;			
		}
		// check if the user releases the 2 key
		else if (GetKeyState(50) < 0x80000000 && m_bIsDown && m_Input == NUM_2)
		{
			m_Input = NONE;
			m_bIsDown = false;
		}
		// Check if the user presses the 2 key
		else if (GetKeyState(50) < 0 && !m_bIsDown)
		{
			m_Input = NUM_2;
			m_bIsInput = true;
			m_bIsDown = true;			
		}
		// check if the user releases the 3 key
		else if (GetKeyState(51) < 0x80000000 && m_bIsDown && m_Input == NUM_3)
		{
			m_Input = NONE;
			m_bIsDown = false;
		}
		// Check if the user presses the 3 key
		else if (GetKeyState(51) < 0 && !m_bIsDown)
		{
			m_Input = NUM_3;
			m_bIsInput = true;
			m_bIsDown = true;			
		}
		// check if the user releases the 4 key
		else if (GetKeyState(52) < 0x80000000 && m_bIsDown && m_Input == NUM_4)
		{
			m_Input = NONE;
			m_bIsDown = false;
		}
		// Check if the user presses the 4 key
		else if (GetKeyState(52) < 0 && !m_bIsDown)
		{
			m_Input = NUM_4;
			m_bIsInput = true;
			m_bIsDown = true;			
		}
		// check if the user releases the 5 key
		else if (GetKeyState(53) < 0x80000000 && m_bIsDown && m_Input == NUM_5)
		{
			m_Input = NONE;
			m_bIsDown = false;
		}
		// Check if the user presses the 5 key
		else if (GetKeyState(53) < 0 && !m_bIsDown)
		{
			m_Input = NUM_5;
			m_bIsInput = true;
			m_bIsDown = true;			
		}
		// check if the user releases the 6 key
		else if (GetKeyState(54) < 0x80000000 && m_bIsDown && m_Input == NUM_6)
		{
			m_Input = NONE;
			m_bIsDown = false;
		}
		// Check if the user presses the 6 key
		else if (GetKeyState(54) < 0 && !m_bIsDown)
		{
			m_Input = NUM_6;
			m_bIsInput = true;
			m_bIsDown = true;			
		}
		// check if the user releases the spacebar key
		else if (GetKeyState(VK_SPACE) < 0x80000000 && m_bIsDown && m_Input == SPACE)
		{
			m_Input = NONE;
			m_bIsDown = false;
		}
		// Check if the user presses the spacebar key
		else if (GetKeyState(VK_SPACE) < 0 && !m_bIsDown)
		{
			m_Input = SPACE;
			m_bIsInput = true;
			m_bIsDown = true;			
		}
		// check if the user releases the UP arrow key
		else if (GetKeyState(VK_UP) < 0x80000000 && m_bIsDown && m_Input == UPARROW)
		{
			m_Input = NONE;
			m_bIsDown = false;
		}
		// Check if the user presses the UP arrow key
		else if (GetKeyState(VK_UP) < 0 && !m_bIsDown)
		{
			m_Input = UPARROW;
			m_bIsInput = true;
			m_bIsDown = true;			
		}
		// check if the user releases the DOWN arrow key
		else if (GetKeyState(VK_DOWN) < 0x80000000 && m_bIsDown && m_Input == DOWNARROW)
		{
			m_Input = NONE;
			m_bIsDown = false;
		}
		// Check if the user presses the DOWN arrow key
		else if (GetKeyState(VK_DOWN) < 0 && !m_bIsDown)
		{
			m_Input = DOWNARROW;
			m_bIsInput = true;
			m_bIsDown = true;			
		}
		// check if the user releases the RIGHT arrow key
		else if (GetKeyState(VK_RIGHT) < 0x80000000 && m_bIsDown && m_Input == RIGHTARROW)
		{
			m_Input = NONE;
			m_bIsDown = false;
		}
		// Check if the user presses the RIGHT arrow key
		else if (GetKeyState(VK_RIGHT) < 0 && !m_bIsDown)
		{
			m_Input = RIGHTARROW;
			m_bIsInput = true;
			m_bIsDown = true;			
		}
		// check if the user releases the LEFT arrow key
		else if (GetKeyState(VK_LEFT) < 0x80000000 && m_bIsDown && m_Input == LEFTARROW)
		{
			m_Input = NONE;
			m_bIsDown = false;
		}
		// Check if the user presses the LEFT arrow key
		else if (GetKeyState(VK_LEFT) < 0 && !m_bIsDown)
		{
			m_Input = LEFTARROW;
			m_bIsInput = true;
			m_bIsDown = true;			
		}
		else if (GetKeyState(VK_ESCAPE) < 0) // escape
		{
			m_Input = EXIT;
			m_bIsInput = true;
		}
	}

	return OK;
}

int CController::StartGame()
{
	// render title first
	m_Render.Render(m_Model);

	bool IsDone = false;

	while(m_Input != EXIT)
	{
		Input();
		IsDone = m_Model.Update(m_Input);
		if (IsDone)
			m_Input = EXIT;
		system("CLS");
		m_Render.Render(m_Model);
	}
	return OK;
}